*===================================================================================*
* Ado-file: 	csxmcs Version 1.0 
* Author: 		Chen Shao(陈志豪)
* Affiliation: 	Lanzhou University
* E-mail: 		chenzhh20@lzu.edu.cn
* License:      Mulan PSL v2
* Date: 		2025/2/17                                 
*===================================================================================*

capture program drop csxmcs
program define csxmcs
	version 14
	 //命令格式: csxmcs y x1 x2…
	syntax varlist(min=1) [if] [in]
	di in blue "晨韶学妹向您报告："
	preserve
	local y : word 1 of `varlist'
	foreach var in `varlist'{
		if "`var'" != "`y'"{
			local x `x' `var'
		}
	}
	capture which esttab //检测是否有esttab命令
    if _rc!=0{
        qui ssc install estout,replace
         //下载esttab命令用于输出
    }
	local cunzhao=1
	while `cunzhao'==1{
		di in white _n _n "一、描述数据"
		su `y' `x'

		di in white _n _n "二、相关性分析"
		qui pwcorr `y' `x',sig
		mat sig=r(sig)
		mat MATC=r(C)
		foreach var in `x'{
			local p=el(sig,rownumb(sig,"`var'"),1)
			local c=el(MATC,rownumb(MATC,"`var'"),1)
			while `p' < 0.05{
				local result "显著"
				local p=0.05
			}
			while `p' > 0.05{
				local result "不显著"
				local p=0.05
			}
			di in white "`y'和`var'相关系数为" `c' ",`result'"
		}

		di in white _n _n "三、OLS基准回归"
		di in white _n "(一)有常数项回归"
		reg `y' `x'
		mat result=r(table)
		estimates store OLS
		di in white "模型的拟合优度为" e(r2)
		di in white "模型的调整的判定系数为" e(r2_a)
		foreach var in `x' _cons{
			local beta=el(result,1,colnumb(result,"`var'"))
			local p=el(result,4,colnumb(result,"`var'"))
			while `p' < 0.05{
				local result "显著"
				local p=0.05
			}
			while `p' > 0.05{
				local result "不显著"
				local p=0.05
			}
			di in white "`var'的回归系数为" `beta' ",`result'"
		}
		di in white _n "(二)无常数项回归"
		reg `y' `x',nocons
		estimates store OLS_noc
		di in white "模型的拟合优度为" e(r2)
		di in white "模型的调整的判定系数为" e(r2_a)
		mat result=r(table)
		foreach var in `x'{
			local beta=el(result,1,colnumb(result,"`var'"))
			local p=el(result,4,colnumb(result,"`var'"))
			while `p' < 0.05{
				local result "显著"
				local p=0.05
			}
			while `p' > 0.05{
				local result "不显著"
				local p=0.05
			}
			di in white "`var'的回归系数为" `beta' ",`result'"
		}
		di in white _n _n "四、计算VIF值"
		di in white "(一)有常数项的OLS回归:"
		qui reg `y' `x'
		estat vif
		qui return list
		foreach var in `x'{
			local number =1
			if r(name_`number') !=""{
				while r(name_`number')!="`var'"{
					local number =`number'+1
				}
				local vif=r(vif_`number')
				local result "不严重"
				while `vif'>10{
					local result "严重"
					local vif=10
				}
				di in white "变量`var'的vif值为" r(vif_`number') "，多重共线性`result'；"
			}
		}
		di in white "(二)无常数项的OLS回归:"
		qui reg `y' `x',nocons
		estat vif,unc

		di in white _n _n "五、RESET检验"
		qui reg `y' `x'
		qui estat ovtest
		local p=r(p)
		while `p'<0.05{
			local result "有高次项遗漏"
			local p=0.05
		}
		while `p'>0.05{
			local result "无高次项遗漏"
			local p=0.05
		}
		di in white "·使用被解释变量拟合值的高次项检验,认为`result'"
		qui reg `y' `x'
		qui estat ovtest,rhs
		local p=r(p)
		while `p'<0.05{
			local result "有高次项遗漏"
			local p=0.05
		}
		while `p'>0.05{
			local result "无高次项遗漏"
			local p=0.05
		}
		di in white "·使用解释变量的高次项检验,认为`result'"

		di in white _n _n "六、信息准则"
		qui reg `y' `x'
		qui estat ic
		mat S=r(S)
		local aic=el(S,1,5)
		local bic=el(S,1,6)
		di in white "有常数项的OLS回归:AIC=" `aic' ",BIC=" `bic'
		qui reg `y' `x',nocons
		qui estat ic
		mat S=r(S)
		local aic=el(S,1,5)
		local bic=el(S,1,6)
		di in white "无常数项的OLS回归:AIC=" `aic' ",BIC=" `bic'

		di in white _n _n "七、观测数据的影响力"
		qui reg `y' `x'
		predict lev1,lev
		qui su lev1
		di in white "·有常数项的OLS回归:lev最大值是其平均值的" r(max)/r(mean) "倍"
		drop lev1
		qui reg `y' `x',nocons
		predict lev2,lev
		qui su lev2
		di in white "·无常数项的OLS回归:lev最大值是其平均值的" r(max)/r(mean) "倍"
		drop lev2
		
		di in white _n _n "八、异方差检验"
		qui reg `y' `x'
		qui estat hettest,iid
		local p=r(p)
		while `p'<0.05{
			local result "存在异方差"
			local yfc=1
			local p=0.05
		}
		while `p'>0.05{
			local result "不存在异方差"
			local yfc=0
			local p=0.05
		}
		di in white "·使用被解释变量的拟合值进行BP检验，`result'"
		qui estat hettest,iid rhs
		local p=r(p)
		while `p'<0.05{
			local result "存在异方差"
			local yfc=1
			local p=0.05
		}
		while `p'>0.05{
			local result "不存在异方差"
			local yfc=0
			local p=0.05
		}
		di in white "·使用所有解释变量进行BP检验，`result'"
		qui estat imtest,white
		qui return list
		local p =r(p)
		while `p' < 0.05{
			local result "存在异方差"
			local yfc=1
			local p=0.05
		}
		while `p' > 0.05{
			local result "不存在异方差"
			local yfc=0
			local p=0.05
		}
		di in white "·对有常数项回归进行怀特检验,`result'"
		qui reg `y' `x',nocons
		qui estat imtest,white
		qui return list
		local p =r(p)
		while `p'<0.05{
			local result "存在异方差"
			local yfc=1
			local p=0.05
		}
		while `p'>0.05{
			local result "不存在异方差"
			local yfc=0
			local p=0.05
		}
		di in white "·对无常数项回归进行怀特检验,`result'"
		local yfc2=0
		
		while `yfc'==1{
			di in white _n _n "九、对异方差的处理"
			di in white _n "(一)异方差稳健标准误回归"
			di in white "1.有常数项、稳健标准误回归:"
			reg `y' `x',r
			estimates store Robust
			di in white "模型的拟合优度为" e(r2)
		di in white "模型的调整的判定系数为" e(r2_a)
		mat result=r(table)
		foreach var in `x'{
			local beta=el(result,1,colnumb(result,"`var'"))
			local p=el(result,4,colnumb(result,"`var'"))
			while `p' < 0.05{
				local result "显著"
				local p=0.05
			}
			while `p' > 0.05{
				local result "不显著"
				local p=0.05
			}
			di in white "`var'的回归系数为" `beta' ",`result'"
		}
			di in white "2.无常数项、稳健标准误回归:"
			reg `y' `x',r nocons
			estimates store Robust_noc
			di in white "模型的拟合优度为" e(r2)
		di in white "模型的调整的判定系数为" e(r2_a)
		mat result=r(table)
		foreach var in `x'{
			local beta=el(result,1,colnumb(result,"`var'"))
			local p=el(result,4,colnumb(result,"`var'"))
			while `p' < 0.05{
				local result "显著"
				local p=0.05
			}
			while `p' > 0.05{
				local result "不显著"
				local p=0.05
			}
			di in white "`var'的回归系数为" `beta' ",`result'"
		}
			di in white _n "(二)FWLS回归"
			di in white "1.有常数项FWLS回归:"
			qui reg `y' `x'
			local r2_con=e(r2)
			qui reg `y' `x',nocons
			local r2_noc=e(r2)
			while `r2_con'>`r2_noc'{
				qui reg `y' `x'
				predict e1,r
				g e2=e1^2
				g lne2=ln(e2)
				local r2_con=0
				local r2_noc=0
			}
			while `r2_con'<`r2_noc'{
				qui reg `y' `x',nocons
				predict e1,r
				g e2=e1^2
				g lne2=ln(e2)
				local r2_con=0
				local r2_noc=0
			}
			qui reg lne2 `x'
			local r2_con=e(r2)
			qui reg lne2 `x',nocons
			local r2_noc=e(r2)
			while `r2_con'>`r2_noc'{
				qui reg lne2 `x'
				predict lne2f
				g e2f=exp(lne2f)
				local r2_con=0
				local r2_noc=0
			}
			while `r2_con'<`r2_noc'{
				qui reg lne2 `x',nocons
				predict lne2f
				g e2f=exp(lne2f)
				local r2_con=0
				local r2_noc=0
			}
			reg `y' `x' [aw=1/e2f]
			estimates store FWLS
			di in white "模型的拟合优度为" e(r2)
		di in white "模型的调整的判定系数为" e(r2_a)
		mat result=r(table)
		foreach var in `x'{
			local beta=el(result,1,colnumb(result,"`var'"))
			local p=el(result,4,colnumb(result,"`var'"))
			while `p' < 0.05{
				local result "显著"
				local p=0.05
			}
			while `p' > 0.05{
				local result "不显著"
				local p=0.05
			}
			di in white "`var'的回归系数为" `beta' ",`result'"
		}
			foreach var in e1 e2 lne2 lne2f e2f{
				drop `var'
			}
			di in white "2.无常数项FWLS回归:"
			qui reg `y' `x'
			local r2_con=e(r2)
			qui reg `y' `x',nocons
			local r2_noc=e(r2)
			while `r2_con'>`r2_noc'{
				qui reg `y' `x'
				predict e1,r
				g e2=e1^2
				g lne2=ln(e2)
				local r2_con=0
				local r2_noc=0
			}
			while `r2_con'<`r2_noc'{
				qui reg `y' `x',nocons
				predict e1,r
				g e2=e1^2
				g lne2=ln(e2)
				local r2_con=0
				local r2_noc=0
			}
			qui reg lne2 `x'
			local r2_con=e(r2)
			qui reg lne2 `x',nocons
			local r2_noc=e(r2)
			while `r2_con'>`r2_noc'{
				qui reg lne2 `x'
				predict lne2f
				g e2f=exp(lne2f)
				local r2_con=0
				local r2_noc=0
			}
			while `r2_con'<`r2_noc'{
				qui reg lne2 `x',nocons
				predict lne2f
				g e2f=exp(lne2f)
				local r2_con=0
				local r2_noc=0
			}
			reg `y' `x' [aw=1/e2f],nocons
			estimates store FWLS_noc
			di in white "模型的拟合优度为" e(r2)
		di in white "模型的调整的判定系数为" e(r2_a)
		mat result=r(table)
		foreach var in `x'{
			local beta=el(result,1,colnumb(result,"`var'"))
			local p=el(result,4,colnumb(result,"`var'"))
			while `p' < 0.05{
				local result "显著"
				local p=0.05
			}
			while `p' > 0.05{
				local result "不显著"
				local p=0.05
			}
			di in white "`var'的回归系数为" `beta' ",`result'"
		}
			foreach var in e1 e2 lne2 lne2f e2f{
				drop `var'
			}
			local yfc=0
			local yfc2=1
		}
		
		while `yfc2'==0{
			di in white _n _n "九、结果汇总"
			esttab OLS OLS_noc,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
			local yfc2=2
		}
		while `yfc2'==1{
			di in white _n _n "十、结果汇总"
			esttab OLS OLS_noc Robust Robust_noc FWLS FWLS_noc,b(3) se(2) sca(r2 N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
			local yfc2=2
		}
		local cunzhao=2
	}
	restore
	di in blue _n "您布置的任务已经完成，请您过目~♥"
end